package Text;

import java.util.Arrays;
class Text2 {
    public static void main(String[] args) {
        int[] arr = {1, 2, 3, 4, 5};
        //数组拷贝
        my_copy(arr, arr.length);
        //数组平均值
        System.out.println(avg(arr));
        //查找
        System.out.println(find(arr, 1));
        //二分查找
        System.out.println(erfen(arr, 1));
        //冒泡排序
        my_sort(arr);
        //数组逆序
        my_reverse(arr);
        //数组转化成字符串
        System.out.println(Arrays.toString(arr));
    }

    public static void my_reverse(int[] arr) {
        int l = 0;
        int r = arr.length - 1;
        while (l < r) {
            int temp = arr[l];
            arr[l] = arr[r];
            arr[r] = temp;
            l++;
            r--;
        }
    }

    static int[] my_copy(int[] arr, int len) {
        int[] newarr = new int[len];
        for (int i = 0; i < len; i++) {
            newarr[i] = arr[i];
        }
        return newarr;
    }

    public static double avg(int[] arr) {
        int sum = 0;
        for (int x : arr) {
            sum += x;
        }
        return (double) sum / (double) arr.length;
    }

    public static int find(int[] arr, int data) {
        for (int i = 0; i < arr.length; i++) {
            if (arr[i] == data) {
                return i;
            }
        }
        return -1;

    }

    static int erfen(int[] arr, int target) {
        int l = 0;
        int r = arr.length - 1;
        while (l <= r) {
            int mid = (l + r) >> 1;
            if (arr[mid] < target) {
                l = mid + 1;
            } else if (arr[mid] == target) {
                return mid;
            } else {
                r = mid - 1;
            }
        }
        return -1;
    }

    public static void my_sort(int[] arr) {
        for (int i = 0; i < arr.length; i++) {
            for (int j = 0; j < arr.length - i - 1; j++) {
                if (arr[j] > arr[j + 1]) {
                    int temp = arr[j];
                    arr[j] = arr[j + 1];
                    arr[j + 1] = temp;
                }
            }
        }
    }
}1